# Load all necessary packages here
library(tidyverse)
library(readxl)
library(tigris)
library(leaflet)
library(htmltools)
library(htmlwidgets)
library(scales)
library(plotly)
library(moderndive)
library(ggpubr)
library(leaflet.extras)
# create a vector of all the states in the West region of the U.S.
West = c("WA", "OR", "ID", "MT", "WY", "CO",
"UT", "NV", "CA", "AK", "HI")
# create a vector of all the states in the Southwest region of the U.S.
Southwest = c("AZ", "NM", "TX", "OK")
# create a vector of all the states in the Southeast region of the U.S.
Southeast = c("AR", "LA", "MS", "AL", "GA", "FL", "SC",
"TN", "NC", "KY", "VA", "WV", "DC", "DE", "MD")
# create a vector of all the states in the Northeast region of the U.S.
Northeast = c("NJ", "CT", "RI", "PA", "NY", "MA", "NH", "VT", "ME")
# create a vector of all the states in the Midwest region of the U.S.
Midwest = c("ND", "SD", "NE", "KS", "MO", "IA", "MN",
"WI", "IL", "IN", "OH", "MI")
# make a dataframe for our first dataset (poverty and household income)
# filter for year 2010
# select variables of importance
# to get ready to join: rename a column
poverty_income <- read_excel("state_series_1980-2014.xls") %>%
filter(year == 2010) %>%
select(fips, state_code, median_hhinc, percent_pov, total_pop) %>%
rename("FIPST" = fips)
# make a dataframe for our second dataset (dropout rates)
# select variables of importance
dropoutRates <- read_excel("sdr091a.xls") %>%
select(FIPST, STATENAME, DRP912,
TOTDHI, TOTDBL, TOTDWH, TOTDAS, TOTDAM)
# join by FIPS state numeric code
# remove District of Columbia
# add a new variable called "region" to the dataframe based on what region the state is in
joinedDataFull <- inner_join(dropoutRates, poverty_income, by = "FIPST") %>%
filter(!(STATENAME == "District of Columbia")) %>%
mutate(region = case_when(state_code %in% West ~ "West",
state_code %in% Southwest ~ "Southwest",
state_code %in% Southeast ~ "Southeast",
state_code %in% Northeast ~ "Northeast",
state_code %in% Midwest ~ "Midwest"))We used the state dropout data1 from the Common Core of Data (CCD) surveys that are submitted every year to the National Center for Education Statistics (NCES).
We also used demographic and economic data2. This data came from the U.S. Bureau of Labor Statistics Local Area Statistics Project, U.S. Census Bureau Small Area Income and Poverty Estimates, and U.S. Census Bureau Population and Housing Estimates. We chose to focus on median household income, poverty level, and total resident population (all people who are usually residents of a specific state3).
We created an interactive stacked barplot4 and chose one common way to divide the United States into five regions5. To easily compare the heights of the different colors between the bars, you can hover your mouse over a color. The popup label will tell you the total population for a given U.S. region and the number of dropouts for a particular race and region.
# group by region
# count: number of Hispanic drop outs, number of Black drop outs,
# number of White drop outs, number of Asian/Hawaiian Native/Pacific Islander drop outs,
# number of American Indian/Alaska Native drop outs, total population
barplotData <- joinedDataFull %>%
group_by(region) %>%
summarize(SUMHI = sum(TOTDHI), SUMBL = sum(TOTDBL), SUMWH = sum(TOTDWH),
SUMAS = sum(TOTDAS), SUMAM = sum(TOTDAM), SUMPOP = sum(total_pop))
# make stacked barplot
plot_ly(data = barplotData, x = ~region, y = ~SUMHI,
type = 'bar', name = 'Hispanic',
text = paste("Total Region Population:", comma(barplotData$SUMPOP)),
marker = list(color = 'rgb(0,0,128)')) %>%
add_trace(y = ~SUMBL, name = 'Black',
marker = list(color = 'rgb(30,144,255)')) %>%
add_trace(y = ~SUMWH, name = 'White',
marker = list(color = 'rgb(135,206,250)')) %>%
add_trace(y = ~SUMAS, name = 'Asian/Hawaiian Native/Pacific Islander',
marker = list(color = 'rgb(0,191,255)')) %>%
add_trace(y = ~SUMAM, name = 'American Indian/Alaska Native',
marker = list(color = 'rgb(20, 106, 162)')) %>%
layout(title ="Total High School Dropouts by Race in the 2009-2010 School Year",
yaxis = list(title = 'Number of Dropouts', tickformat = ",d"),
xaxis = list(title = 'U.S. Region', categoryorder = "array",
categoryarray = c("West", "Southeast", "Midwest",
"Southwest", "Northeast")),
barmode = 'stack',
legend = list(x = 100, y = 0.5)) %>%
config(displayModeBar = FALSE)The descending order of the barplot6 allows us to easily see that the West has the largest number of dropouts and the Northeast has the fewest. However, this could be due to the fact that the West has a larger population than the Northeast. However, this reasoning doesn’t explain why the West has more dropouts than the Southeast or Midwest since both of these regions have larger populations than the West.
We created an interactive choropleth map7 which is colored by 2009-2010 dropout rates. Clicking on a state will allow you to see the state’s total population, high school dropout rate, percent of people living in poverty, and the median household income. We start our interactive map centered on the U.S. at zoom level 4. The icon with four arrows8 will bring you back to to this setting.
# load spatial data
states <- states()
# inner join spatial data and a dataframe
states_merged <- geo_join(states, joinedDataFull, "STUSPS", "state_code", how = "inner")
# make blue color palette based on the range of dropout rate numbers
pal_DRP912 <- colorNumeric("Blues", domain=states_merged$DRP912)
# make popup labels
popup_label <- paste0("<strong>", states_merged$NAME,
"</strong><br />Total Population: ",
comma(states_merged$total_pop),
"<br />Dropout Rate: ",
paste(format(round(states_merged$DRP912, 2), nsmall = 2), "%",
sep = ""),
"<br />Percent Poverty: ",
paste(states_merged$percent_pov, "%", sep = ""),
"<br />Median Household Income: ",
comma(states_merged$median_hhinc))# make interactive map
# at start: center the map on the U.S.
# add Earth icon to set to zoom level 4
leaflet(states) %>%
addProviderTiles("CartoDB.Positron") %>%
setView(-98.483330, 38.712046, zoom = 4) %>%
addPolygons(data = states_merged,
fillColor = ~pal_DRP912(states_merged$DRP912),
fillOpacity = 0.7,
weight = 0.2,
smoothFactor = 0.2,
highlight = highlightOptions(weight = 5, color = "#666",
fillOpacity = 0.7, bringToFront = TRUE),
popup = ~popup_label,
label = states_merged$NAME) %>%
addLegend(pal = pal_DRP912,
values = states_merged$DRP912,
position = "bottomright",
title = "Dropout Rate",
labFormat = labelFormat(suffix = "%")) %>%
addResetMapButton()